package com.lgw.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lgw.demo.pojo.News;
import com.lgw.demo.pojo.User;

import javax.xml.ws.ResponseWrapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

//控制层
@Controller
public class UserController {

    @RequestMapping("getuser")
    @ResponseBody
    public User getUser(){
        User user=new User();
        user.setId(1);
        user.setAge(20);
        user.setName("user");
        return user;
    }

    @RequestMapping("login")
public String getLogin(Model model){
        List<News> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            News news = new News();
            news.id = i;
            news.title = "新闻" + i;
            news.content = "这是第" + i + "条新闻";
            list.add(news);
        }
        model.addAttribute("list", list);
//        model.addAttribute("newsid", newsid);
    return "login.html";
    }

    @RequestMapping("commit")       //登录页面判断
    public String commit(Model model, String username, String password) {
        if (username.equals("jwky") && password.equals("123456"))
            return "success.html";
        else return "fail.html";
    }

    @RequestMapping("test")
    public String test(){
        return "test.html";
    }

    @RequestMapping("file")//进入上传文件页面
    public String file(){
        return "file.html";
    }

    //file指定路径
    String uploadPath="E:/data";
    @RequestMapping("filecommit")
    public String filecommit(MultipartFile file){

        File file1 = new File(uploadPath);
        //文件载数据库中只保存路径
        String name= UUID.randomUUID().toString()+file.getOriginalFilename();
        try {
            file.transferTo(new File(file1,name));
            return "upsuccess.html";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail.html";
    }

    //mybatis整合 单表增删改查 多表增删改查
    //每张表都看成单表，通过字段来维护


}


